/**
 * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.addon.securityinformation.controller;

import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;

import io.jboot.db.model.Columns;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jboot.web.validate.EmptyValidate;
import io.jboot.web.validate.Form;
import io.jpress.addon.securityinformation.model.JpressAddonSecurityinformationMfdr;
import io.jpress.addon.securityinformation.service.JpressAddonSecurityinformationMfdrService;
import io.jpress.core.menu.annotation.AdminMenu;
import io.jpress.web.base.AdminControllerBase;

import java.util.Date;


@RequestMapping(value = "/admin/securityinformation/jpress_addon_securityinformation_mfdr", viewPath = "/")
public class _JpressAddonSecurityinformationMfdrController extends AdminControllerBase {

    @Inject
    private JpressAddonSecurityinformationMfdrService service;

    @AdminMenu(text = "使用困难报告管理", groupId = "securityinformation")
    public void index() {

        String report_number = getPara("report_number");
        String aircraft_registration_number = getPara("aircraft_registration_number");
        Date date_submission_start = getParaToDate("date_submission_start");
        Date date_submission_end = getParaToDate("date_submission_end");
        String air_operator = getPara("air_operator");
        String model = getPara("model");
        Columns eq = Columns.create()
                .eq("model",model)
                .eq("report_number", report_number)
                .eq("aircraft_registration_number", aircraft_registration_number)
                .ge("date_submission", date_submission_start).le("date_submission", date_submission_end)
                .eq("air_operator", air_operator);

        Page<JpressAddonSecurityinformationMfdr> entries=service.paginateByColumns(getPagePara(), 10,eq,"date_submission desc");
        setAttr("page", entries);
        render("views/jpress_addon_securityinformation_mfdr_list.html");
    }

    public void edit() {
        int entryId = getParaToInt(0, 0);
        JpressAddonSecurityinformationMfdr entry = entryId > 0 ? service.findById(entryId) : null;
        setAttr("entry", entry);
        render("views/jpress_addon_securityinformation_mfdr_edit.html");
    }

    public void doSave() {
        JpressAddonSecurityinformationMfdr entry = getModel(JpressAddonSecurityinformationMfdr.class,"jpressAddonSecurityinformationMfdr");
        service.saveOrUpdate(entry);
        renderJson(Ret.ok().set("id", entry.getId()));
    }

    public void doDel() {
        Long id = getIdPara();
        render(service.deleteById(id) ? Ret.ok() : Ret.fail());
    }

    @EmptyValidate(@Form(name = "ids"))
    public void doDelByIds() {
        service.batchDeleteByIds(getParaSet("ids").toArray());
        renderOkJson();
    }
}
